package com.quickcancellation.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.quickcancellation.crm.mapper.RoleMapper;
import com.quickcancellation.crm.pojo.entity.Menu;
import com.quickcancellation.crm.pojo.entity.Role;
import com.quickcancellation.crm.pojo.vo.LeftMenuVo;
import com.quickcancellation.crm.pojo.vo.MenuVo;
import com.quickcancellation.crm.pojo.vo.RoleVo;
import com.quickcancellation.crm.service.IMenuService;
import com.quickcancellation.crm.service.IRoleService;
import com.quickcancellation.crm.utils.JSONUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author baomidou
 * @since 2023-02-25
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {


    @Resource
    private RoleMapper roleMapper;

    @Resource
    private IMenuService menuService;


    @Override
    public List<String> getRoleListByUsernumber(String usernumber) {
        return roleMapper.getUserRoleList(usernumber);
    }

    @Override
    public List<RoleVo> getRoleVoByUsernumber(String usernumber) {

        // 所有用户角色
        List<Role> roleList = this.list();

        // 用户所拥有的角色
        List<Role> userRoleList = roleMapper.getRoleListByUsernumber(usernumber);

        // 返回给前端视图的用户角色列表
        List<RoleVo> roleVoList = new ArrayList<>();


        for (Role role : roleList) {

            RoleVo roleVo = new RoleVo();
            roleVo.setRoleName(role.getRolename());
            roleVo.setRoleId(role.getId());
            roleVo.setCheck(this.containRole(role.getId(), userRoleList));

            roleVoList.add(roleVo);
        }


        return roleVoList;
    }

    /**
     * 判断 userRoleList 中是否包含 roleId
     *
     * @param roleId       用户id
     * @param userRoleList 用户列表
     * @return 是否存在
     */
    public boolean containRole(Long roleId, List<Role> userRoleList) {

        for (Role role : userRoleList) {
            if (Objects.equals(role.getId(), roleId)) {
                return true;
            }
        }
        return false;

    }

    @Override
    public boolean addOrDelUserRole(String usernumber, List roleList, String currentUserNumber) {

        for (Object obj : roleList) {
            RoleVo roleVo = JSONUtils.getEntity(obj, RoleVo.class);

            if (roleVo.isCheck()) {
                // 添加
                int result = roleMapper.addUserRole(usernumber, roleVo.getRoleId(), currentUserNumber);
                if (result == 0) {
                    return false;
                }
            } else {
                // 删除
                roleMapper.deleteUserRole(usernumber, roleVo.getRoleId());
            }
        }
        return true;
    }

    @Override
    public List<MenuVo> getMenuByRoleId(String roleId) {
        // 查询所有的权限
        List<Menu> menuList = menuService.list();

        // 根据角色id查询所有的权限
        List<Long> roleMenuList = roleMapper.getRoleMenuByRoleId(roleId);

        List<MenuVo> menuVoList = new ArrayList<>();

        // 创建用户视图列表
        for (Menu menu : menuList) {
            MenuVo menuVo = new MenuVo();
            menuVo.setMenuName(menu.getMenuType());
            menuVo.setMenuId(menu.getId());
            menuVo.setCheck(roleMenuList.contains(menu.getId()));
            menuVoList.add(menuVo);
        }

        return menuVoList;
    }

    @Override
    public boolean saveOrDelRoleMenu(Long id, List<MenuVo> menuVoList) {


        for (MenuVo menuVo : menuVoList) {
            if (menuVo.isCheck()) {
                roleMapper.saveRoleMenu(id, menuVo.getMenuId());
            } else {
                roleMapper.deleteRoleMenu(id, menuVo.getMenuId());
            }
        }

        return true;
    }

    @Override
    public boolean existRoleByRoleNameOrRoleType(String roleName, String roleType) {
        return (roleMapper.exists(new QueryWrapper<Role>().eq("rolename", roleName)) || roleMapper.exists(new QueryWrapper<Role>().eq("roleType", roleType)));
    }

    @Override
    public List<LeftMenuVo> getMenuListByUsernumber(String userumber) {


        List<Menu> originMenuList = roleMapper.getMenuListByUsernumber(userumber);

        List<LeftMenuVo> menuList = new ArrayList<>();


        for (Menu menu : originMenuList) {
            // 如果是父节点
            if (menu.getParentId() == 0) {
                LeftMenuVo parent = new LeftMenuVo();
                // 获取父节点id
                Long parentId = menu.getId();
                List<LeftMenuVo> menus = new ArrayList<>();
                // 遍历子节点
                for (Menu tempMenu : originMenuList) {
                    // 是该父节点
                    if (Objects.equals(tempMenu.getParentId(), parentId)) {
                        LeftMenuVo children = new LeftMenuVo();
                        children.setUrl(tempMenu.getUrl());
                        children.setMenuId(tempMenu.getId());
                        children.setIcon(tempMenu.getIcon());
                        children.setHasThird(tempMenu.getHasThird());
                        children.setMenuName(tempMenu.getMenuType());
                        menus.add(children);
                    }
                }
                parent.setMenuName(menu.getMenuType());
                parent.setMenuId(menu.getId());
                parent.setIcon(menu.getIcon());
                parent.setHasThird(menu.getHasThird());
                parent.setMenuName(menu.getMenuType());
                parent.setMenus(menus);

                menuList.add(parent);
            }
        }
        return menuList;
    }

    @Override
    public String getDefaultMenu(String usernumber) {
        return roleMapper.getDefaultMenu(usernumber);
    }
}
